<style scoped lang="scss">
@import url('index.scss');
</style>
<template>
  <div class="app-container">
    <!--新模版样式-->
    <div class="topsearch">
      <el-row>
        <el-col :span="22" class="searchcol">
          <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
            <el-form-item label="采购单" prop="purchaseid">
              <el-input v-model="queryParams.purchaseid" placeholder="请输入采购单ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="药品ID" prop="drugid">
              <el-input v-model="queryParams.drugid" placeholder="请输入药品ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="刻度时间(厂家)" prop="calibrationTime">
        <el-date-picker clearable
          v-model="queryParams.calibrationTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择刻度时间(厂家)">
        </el-date-picker>
      </el-form-item> -->
            <!-- <el-form-item label="刻度活度(MBq)" prop="activityMbq">
        <el-input
          v-model="queryParams.activityMbq"
          placeholder="请输入刻度活度(MBq)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
            <!-- <el-form-item label="体积(mL)" prop="volumeMl">
        <el-input
          v-model="queryParams.volumeMl"
          placeholder="请输入体积(mL)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
            <!-- <el-form-item label="数量" prop="unitNum">
        <el-input
          v-model="queryParams.unitNum"
          placeholder="请输入数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
            <!-- <el-form-item label="单价(元/MBq)" prop="unitPrice">
        <el-input
          v-model="queryParams.unitPrice"
          placeholder="请输入单价(元/MBq)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
            <!-- <el-form-item label="金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
            <el-form-item label="铅罐编号" prop="leadPotNo">
              <el-input v-model="queryParams.leadPotNo" placeholder="请输入铅罐编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="运输单号" prop="transportNo">
              <el-input v-model="queryParams.transportNo" placeholder="请输入运输单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button class="newButton" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22" class="rightcol">
          <div class="newbtn" @click="handleAdd" v-hasPermi="['system:item:add']">
            <img src="@/assets/images/add.svg" alt="">
            <span>新建</span>
          </div>
          <div class="newbtn" v-if="single == false" @click="handleUpdate" v-hasPermi="['system:item:edit']">
            <img src="@/assets/images/edit.svg" alt=""></img>
            <span>编辑</span>
          </div>
          <div class="newbtn" v-if="multiple == false" @click="handleDelete" v-hasPermi="['system:item:remove']">
            <img src="@/assets/images/del.svg" alt=""></img>
            <span>删除</span>
          </div>
          <div class="newbtn" @click="handleExport" v-hasPermi="['system:item:export']">
            <img src="@/assets/images/export.svg" alt=""></img>
            <span>导出</span>
          </div>
          <div class="newbtn" @click="getList">
            <img src="@/assets/images/refresh.svg" alt=""></img>
            <span>刷新</span>
          </div>
        </el-col>
      </el-row>
    </div>


    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:item:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:item:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:item:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="明细ID" align="center" prop="itemid" />
      <el-table-column label="采购单ID" align="center" prop="purchaseid" />
      <el-table-column label="药品ID" align="center" prop="drugid" />
      <el-table-column label="刻度时间(厂家)" align="center" prop="calibrationTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.calibrationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="刻度活度(MBq)" align="center" prop="activityMbq" />
      <el-table-column label="体积(mL)" align="center" prop="volumeMl" />
      <el-table-column label="数量" align="center" prop="unitNum" />
      <el-table-column label="单价(元/MBq)" align="center" prop="unitPrice" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="铅罐编号" align="center" prop="leadPotNo" />
      <el-table-column label="铅罐编号图片" align="center" prop="leadPotImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.leadPotImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="运输单号" align="center" prop="transportNo" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:item:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:item:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改采购明细对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" class="newDialog">
      <el-form ref="itemRef" :model="form" :rules="rules" label-width="120px">

        <el-row :gutter="20">

          <el-col :span="12">
            <el-form-item label="申请日期" prop="purchaseDate">
              <!-- <el-input v-model="form.purchaseDate" placeholder="请输入采购申请" /> -->

            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="采购单ID" prop="purchaseid">
              <el-input v-model="form.purchaseid" placeholder="请输入采购单ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="药品ID" prop="drugid">
              <el-input v-model="form.drugid" placeholder="请输入药品ID" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="刻度时间(厂家)" prop="calibrationTime">
              <el-date-picker clearable v-model="form.calibrationTime" type="date" value-format="YYYY-MM-DD"
                placeholder="请选择刻度时间(厂家)">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="刻度活度(MBq)" prop="activityMbq">
              <el-input v-model="form.activityMbq" placeholder="请输入刻度活度(MBq)" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="体积(mL)" prop="volumeMl">
              <el-input v-model="form.volumeMl" placeholder="请输入体积(mL)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量" prop="unitNum">
              <el-input v-model="form.unitNum" placeholder="请输入数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="单价(元/MBq)" prop="unitPrice">
              <el-input v-model="form.unitPrice" placeholder="请输入单价(元/MBq)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="金额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入金额" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">

          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">

          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>


        <el-form-item label="铅罐编号" prop="leadPotNo">
          <el-input v-model="form.leadPotNo" placeholder="请输入铅罐编号" />
        </el-form-item>
        <el-form-item label="铅罐编号图片" prop="leadPotImage">
          <image-upload v-model="form.leadPotImage" />
        </el-form-item>
        <el-form-item label="运输单号" prop="transportNo">
          <el-input v-model="form.transportNo" placeholder="请输入运输单号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" :rows="4" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Item">
import { listItem, getItem, delItem, addItem, updateItem } from "@/api/system/item"
import { listPurchase } from "@/api/system/purchase" //查询采购单列表@鲍小伟

const { proxy } = getCurrentInstance()

const itemList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    purchaseid: null,
    drugid: null,
    calibrationTime: null,
    activityMbq: null,
    volumeMl: null,
    unitNum: null,
    unitPrice: null,
    amount: null,
    leadPotNo: null,
    leadPotImage: null,
    transportNo: null,
    status: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

//采购订单列表
const purchaseList = ref([])


/** 查询采购明细列表 */
function getList() {
  loading.value = true
  listItem(queryParams.value).then(response => {
    itemList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    itemid: null,
    purchaseid: null,
    drugid: null,
    calibrationTime: null,
    activityMbq: null,
    volumeMl: null,
    unitNum: null,
    unitPrice: null,
    amount: null,
    leadPotNo: null,
    leadPotImage: null,
    transportNo: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("itemRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.itemid)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加采购明细"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _itemid = row.itemid || ids.value
  getItem(_itemid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改采购明细"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["itemRef"].validate(valid => {
    if (valid) {
      if (form.value.itemid != null) {
        updateItem(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addItem(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _itemids = row.itemid || ids.value
  proxy.$modal.confirm('是否确认删除采购明细编号为"' + _itemids + '"的数据项？').then(function() {
    return delItem(_itemids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/item/export', {
    ...queryParams.value
  }, `item_${new Date().getTime()}.xlsx`)
}

getList()
</script>
